package cn.net.withub.dataCollector.web.utils;

import com.sun.corba.se.spi.orb.DataCollector;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.library.UserDefineLibrary;
import org.ansj.recognition.arrimpl.AsianPersonRecognition;
import org.ansj.recognition.arrimpl.ForeignPersonRecognition;
import org.ansj.recognition.arrimpl.NumRecognition;
import org.ansj.recognition.arrimpl.UserDefineRecognition;
import org.ansj.splitWord.analysis.DicAnalysis;
import org.ansj.splitWord.analysis.IndexAnalysis;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.ansj.util.Graph;
import org.ansj.util.MyStaticValue;
import org.ansj.util.NameFix;
import org.junit.Test;
import org.nlpcn.commons.lang.tire.domain.Forest;
import org.nlpcn.commons.lang.tire.library.Library;

/**
 * 5.0.2版本
 */
public class ansjTest {
    public static void test1(){
        //只关注这些词性的词
        Set<String> expectedNature = new HashSet<String>() {{
            add("n");add("v");add("vd");add("vn");add("vf");
            add("vx");add("vi");add("vl");add("vg");
            add("nt");add("nz");add("nw");add("nl");
            add("ng");add("userDefine");add("wh");
        }};
        String str = "欢迎使用ansj_seg,(ansj中文分词)在这里如果你遇到什么问题都可以联系我.我一定尽我所能.帮助大家.ansj_seg更快,更准,更自由!" ;
        Result result = ToAnalysis.parse(str); //分词结果的一个封装，主要是一个List<Term>的terms
        System.out.println(result.getTerms());
        List<Term> terms = result.getTerms(); //拿到terms
        System.out.println(terms.size());

        for(int i=0; i<terms.size(); i++) {
            String word = terms.get(i).getName(); //拿到词
            String natureStr = terms.get(i).getNatureStr(); //拿到词性
            if(expectedNature.contains(natureStr)) {
                System.out.println(word + ":" + natureStr);
            }
        }
    }
    public static void testDic() throws Exception{
        Forest forest0 = Library.makeForest("E:\\webProject\\DataCollector\\DataCollector\\DataCollector-web\\src\\main\\resources\\library\\ambiguity.dic");
        Result re = DicAnalysis.parse("孙杨在里约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军", forest0);
        System.out.println(re);
          //上面这个没能导入自定义词典
        //成功
        UserDefineLibrary.insertWord("ansj中文分词", "userDefine", 1000);
        Result terms = ToAnalysis.parse("我觉得Ansj中文分词是一个不错的系统!我是王婆!");
        System.out.println("增加新词例子:" + terms);
        // 删除词语,只能删除.用户自定义的词典.
        UserDefineLibrary.removeWord("ansj中文分词");
        terms = ToAnalysis.parse("我觉得ansj中文分词是一个不错的系统!我是王婆!");
        System.out.println("删除用户自定义词典例子:" + terms);

        UserDefineLibrary.insertWord("奥运会男子200米自由泳", "test", 1000);
        UserDefineLibrary.insertWord("奥运会男子", "test1", 1000);
        terms = ToAnalysis.parse("孙杨在里约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军");
        Result  terms1 = DicAnalysis.parse("孙杨在里约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军");
        Result  terms2 = IndexAnalysis.parse("孙杨在里约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军");
        String str = "孙杨在里约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军?我觉得Ansj中文分词是一个不错的系统!我是王婆'以应用到自然语言处理等方面,适用于对分词效果要求高的各种项目.";
        Result terms3 = ToAnalysis.parse(str);

        System.out.println("删除用户自定义词典例子:" + terms);
        System.out.println("删除用户自定义词典例子:" + terms1);
        System.out.println("删除用户自定义词典例子:" + terms2);

    }
    public static void main(String[] args) {
        //test1();
        try {
            testDic();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void wordTest(){
       /* String str = "这是一段测试文字";
        DicLibrary.insert(DicLibrary.DEFAULT, "这是");//设置自定义分词
        Result result= NlpAnalysis.parse(str);
        List<Term> termList=result.getTerms();
        for(Term term:termList){
            System.out.println(term.getName()+":"+term.getNatureStr());
        }*/
    }
    @Test
    public void wordTest1() throws Exception {
        //E:\webProject\DataCollector\DataCollector\DataCollector-web\library\test.dic
        Forest forest0 = Library.makeForest("E:\\webProject\\DataCollector\\DataCollector\\DataCollector-web\\library\\ambiguity.dic");
        //Forest forest0 = Library.makeForest("E:\\webProject\\DataCollector\\DataCollector\\DataCollector-web\\src\\main\\resources\\library\\test.dic");
        Result re = DicAnalysis.parse("潇洒的罗文军孙杨在罗文礼奇约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军", forest0);
        System.out.println(re);
    }

    //通过配置文件
    @Test
    public void wordTest2() throws Exception {
        //Forest forest0 = Library.makeForest("E:\\webProject\\DataCollector\\DataCollector\\DataCollector-web\\library\\ambiguity.dic");
        //Forest forest0 = Library.makeForest("E:\\webProject\\DataCollector\\DataCollector\\DataCollector-web\\src\\main\\resources\\library\\test.dic");
        //Result re = DicAnalysis.parse("潇洒的罗文军孙杨在罗文礼奇约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军", forest0);
        Result re = DicAnalysis.parse("潇洒的罗文军孙杨在罗文礼奇约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军");
        System.out.println(re);
    }
    //通过代码方式来加载
    @Test
    public void wordTest3() throws Exception {
        //Forest forest0 = Library.makeForest("E:\\webProject\\DataCollector\\DataCollector\\DataCollector-web\\library\\ambiguity.dic");
        //Forest forest0 = Library.makeForest("E:\\webProject\\DataCollector\\DataCollector\\DataCollector-web\\src\\main\\resources\\library\\test.dic");
        //Result re = DicAnalysis.parse("潇洒的罗文军孙杨在罗文礼奇约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军", forest0);
        //Result re = DicAnalysis.parse("潇洒的罗文军孙杨在罗文礼奇约奥运会男子200米自由泳决赛中，以1分44秒65夺得冠军");
        //System.out.println(re);
        //// 关闭名字识别
        //MyStaticValue.isNameRecognition = false;
        //// 配置自定义词典的位置。注意是绝对路径
        //MyStaticValue.ENV.put(DicLibrary.DEFAULT, "D:\\project\\library\\default.dic");
        //
        //String text = "基于Java开发的轻量级的中分分词工具包";
        //
        //System.out.println(DicAnalysis.parse(text));

    }
}
